/**
 * @file
 * @auth jinguangguo
 * @date 2017/2/21
 */

import Vue from 'vue';

const REG_NUMBER = /^[1-9][0-9]{0,5}(\.\d{0,6})?$/;
const REG_DECIMAL = /^0(\.\d{0,6})?$/;

Vue.directive('number6', {
    deep: true,
    bind(el, binding, vnode) {
        let scope = vnode.context;
        let vModel = binding.expression;
        let arrs = vModel.split('.');
        let attr0 = arrs[0];
        let attr1 = arrs[1];

        if (vModel) {
            scope.$watch(vModel, (newVal, oldVal) => {
                if (newVal !== '') {
                    if (REG_NUMBER.test(newVal) === true || REG_DECIMAL.test(newVal) === true) {

                    } else {
                        if (attr1) {
                            scope[attr0][attr1] = oldVal;
                        } else {
                            scope[attr0] = oldVal;
                        }
                    }

                }
            }, {deep: true});
        }
    },
    update(el, binding, vnode) {

    }
});